// SEE END OF FILE FOR LICENSE TERMS

/****************************************************************************
 ServoInterface -
 Intended as an interface block for radio control servos and receivers.

 Its main objective is to communicate with other controllers which have the
 ability to use the radio inputs and control the servo outputs

 One focus is to use CANbus to communcate with the interface.  This is not a
 demand or a limitation.

 The hardware is based on a dsPIC30F6010A processor.
 
 Main discussion forum is here 
  http://groups.google.com/group/rc-servo-interface
*****************************************************************************/

#include "defines.h"

#include <string.h>

//	main program for testing the IMU.
//	Select device options:

_FOSC(CSW_FSCM_OFF & XT_PLL4);		// 16MHz clock with 4MHz crystal or resonator
//_FWDT( WDT_OFF ) ;				// no watchdog timer
_FWDT(WDT_ON & WDTPSA_8 & WDTPSB_12);
_FBORPOR( 	PBOR_OFF & // brown out detection off
			//PBOR_ON & // brown out detection on
			//BORV_27 & // brown out triggered at 2.7V
			//PWRT_64 & // powerup timer at 64ms
			MCLR_EN &  // enable MCLR
			RST_PWMPIN & // pwm pins as pwm
			PWMxH_ACT_HI & // PWMH is active high
			PWMxL_ACT_HI 
						) ; // PMWL is active high
_FGS( CODE_PROT_OFF ) ; // no protection
//_FICD( 0xC003 ) ;		// normal use of debugging port
_FICD( ICS_PGD ) ;		// normal use of debugging port

void init_all(void)
{
	init_capture() ; 		// initialize capture module
	init_pwm() ;			// set up the PWM outputs
	init_states() ;
	init_cntrl_comms();		// Initialise whatever the control comms interface is
	init_heartbeat();
//	init_USART1() ;

	return ;
}

void init_states(void)
{
	op_status.passthrough   = 0;
	op_status.rx_ok 		= 0;
	op_status.cntrl_comm_ok	= 0;
};

int main (void)
{	//	Initialize all modules and turn on the interrupts.
	defaultCorcon = CORCON;
	init_all() 		;   // initialize interrupt handlers
	SRbits.IPL = 0 ;	// turn on all interrupt priorities
	
	while (1)			//  nothing else to do...entirely interrupt driven
	{
	}
	return 0 ;
}


// Interface recieves servo data from autopilot
void receive_servo_data()
{
	int index;
	unsigned int ServoPW;

	if(ServoInDataBuff[THROTTLE_OUTPUT_CHANNEL] < THROTTLE_MINIMUM_OUTPUT)
		ServoInDataBuff[THROTTLE_OUTPUT_CHANNEL] = THROTTLE_MINIMUM_OUTPUT;

	RxTimeouts = 0;
};


/****************************************************************************/
// This is part of the servo and radio interface software
//
// ServoInterface source code
//	http://code.google.com/p/rc-servo-interface
//
// Copyright 2010 ServoInterface Team
// See the AUTHORS.TXT file for a list of authors of ServoInterface.
//
// ServoInterface is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ServoInterface is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License 
// along with ServoInterface.  If not, see <http://www.gnu.org/licenses/>.
//
// Many parts of ServoInterface use either modified or unmodified code
// from the MatrixPilot pilot project.
// The project also contains code for modifying MatrixPilot to operate
// with ServoInterface.
// For details, credits and licenses of MatrixPilot see the AUTHORS.TXT file.
// or see this website: http://code.google.com/p/gentlenav
/****************************************************************************/
